import importlib
import os
import time
import uuid
import datetime

MINUTE_SECONDS = 60
HOUR_SECONDS = 60*60
DAY_SECONDS = 60*60*24

class time_rest(object):
    def __init__(self, seconds):
        self.days = int(seconds // DAY_SECONDS)
        self.hours = int((seconds - self.days * DAY_SECONDS) / HOUR_SECONDS)
        self.minutes = int((seconds - self.days * DAY_SECONDS - self.hours * HOUR_SECONDS) / MINUTE_SECONDS)

int_2_2d = lambda i: str(i) if i >= 10 < 100 else '0'+str(i)[0]
int_2_nd = lambda i, n: '0'*(n-len(str(i)))+str(i) if n >= len(str(i)) else str(i)[0:n]

def int_2_nd_generator(n):
    int_2_nd = lambda i: '0'*(n-len(str(i)))+str(i) if n >= len(str(i)) else str(i)[0:n]
    return int_2_nd

def get_uuid_str():
    """
    Get UUID str
    :return:
    """
    return uuid.uuid4().hex


def get_time_stamp():
    """
    Get now timestamp
    :return:
    """
    return time.time()


def get_settings():
    """
    Get settings Module
    :return:
    """
    settings_module = os.environ['DJANGO_SETTINGS_MODULE']
    settings = importlib.import_module(settings_module)
    return settings


def is_time(buffer):
    """
    判断字符串是不是时间
    :param buffer:
    :param spliter: 时间字段分隔符
    :return:
    """
    pieces = buffer.strip().split(':')
    if len(pieces) != 2:
        return False
    else:
        hour, minute = pieces
        if int(hour)>=0 and int(hour)<=23 and int(minute)>=0 and int(minute)<=59:
            return True
        else:
            return False


def is_time_range(buffer):
    """
    判断字符串是不是时间段(07:12-08:12)
    :param buffer:
    :param spliter: 时间字段分隔符
    :param spliter1: 时间分隔符
    :return:
    """
    time_pieces = buffer.strip().split('-')
    if len(time_pieces) != 2:
        return False
    else:
        time1, time2 = time_pieces
        if is_time(time1) and is_time(time2):
            if datetime.datetime.strptime(time1, '%H:%M') < datetime.datetime.strptime(time2, '%H:%M'):
                return True
            else:
                return False
        else:
            return False


def get_age(birthday):
    """
    从出生日期转年龄
    :param birthday:  datetime type
    :return:
    """
    today = datetime.date.today()
    age = today.year - birthday.year
    if today.month - birthday.month < 0:  # 当前月份小于生日月份时就可以肯定当前年的生日还未过，此时年龄应减1岁
        age -= 1
    if today.month - birthday.month == 0:  # 等于0就表示月份相同，就得看日是否相同
        if today.day - birthday.day < 0:  # 当前日小于生日的日就可以肯定当前年的生日还未过，此时应减1岁
            age -= 1
    if age < 0:  # 这个必须放在最后判断
        age = 0
    return age

chars = [ chr(65+i) for i in range(26)] + [chr(97+i) for i in range(26)] + [str(i) for i in range(10)]


